package com.liang.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liang.mall.entity.Category;
import com.liang.mall.util.ResultEntity;
import com.liang.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 商品三级分类 前端控制器
 * </p>
 *
 * @author liang
 * @since 2021-05-17
 */
@Controller
@RequestMapping("/category")
public class CategoryController {


    @Autowired
    CategoryService categoryService;


    @GetMapping("/list")
    @ResponseBody
    public ResultEntity list(){

       List<Category> categoryList = categoryService.getAllCatagory();

        return ResultEntity.ok(categoryList);

    }

    @PostMapping("/delete/{id}")
    @ResponseBody
    public ResultEntity delete(@PathVariable("id") Integer id){

        boolean b = categoryService.removeById(id);

        return b? ResultEntity.ok():ResultEntity.error("删除失败");

    }


    @RequestMapping("/update")
    @ResponseBody
    public ResultEntity update(@RequestBody Category category){

        System.out.println("category = " + category);

        boolean update = categoryService.update(category, new QueryWrapper<Category>().eq("category_id", category.getCategoryId()));

        return update? ResultEntity.ok():ResultEntity.error("更新失败");


    }

    @ResponseBody
    @PostMapping("/add")
    public ResultEntity add(@RequestBody Category category){

        boolean save = categoryService.save(category);

        return save?ResultEntity.ok():ResultEntity.error();

    }

}

